// Copyright (C) 2014 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 12.3.7
description: >
    Tagged template application takes precedence over `new` invocation.
---*/

function Test() {
    function Constructor(x) {
      arg = x;
    }
    var tag = function(x) {
      templateObject = x;
      return Constructor;
    };
    var arg = null;
    var instance, templateObject;

    instance = new tag`first template`;

    assert(instance instanceof Constructor);
    assert.sameValue(templateObject[0], 'first template');
    assert.sameValue(arg, undefined);

    instance = new tag`second template`('constructor argument');
    assert.sameValue(templateObject[0], 'second template', 'tagging function');
    assert.sameValue(arg, 'constructor argument');
}

// preheat
const NUMBER_OF_PREHEAT = 1;
for (let i = 0; i < NUMBER_OF_PREHEAT; i++) {
    Test();
}

ArkTools.jitCompileAsync(Test);
assert.sameValue(ArkTools.waitJitCompileFinish(Test), true, '[JIT] JIT disabled.');

// test
const NUMBER_OF_TEST = 1; // or 2
for (let i = 0; i < NUMBER_OF_PREHEAT; i++) {
    Test();
}
